--[[
* 属性类光环
*]]
---@class BuffEffect67 : BaseBuffEffect
BuffEffect67 = ClientFight.CreateClass("BuffEffect67", ClientFight.BaseBuffEffect)
local t = BuffEffect67
---@param fight Fight
---@param buff FighterBuff
function t:action(fight, buff, ...)
    local source = buff.source;
    local target = buff.target;
    if (source == target) then
        self:masterValid(fight, buff);
    else
        self:guestValid(fight, buff);
    end
    return IBuffEffect.FAIL;
end

function t:canAddBuffForce(buff)
    if (buff.source == buff.target) then
        --光环类技能,施法者自己,100%能添加成功
        return true;
    end
    return ClientFight.BaseBuffEffect.canAddBuffForce(self, buff);
end

function t:afterAdd(fight, buff)
    local source = buff.source;
    local target = buff.target;
    if (source == target) then
        local childId = buff.buffBean.f_BuffEffectValue[2];
        if (childId > 0) then
            --光环主人,直接给自己加个buff
            BuffManager.addBuff(fight, buff.skill, source, source, false, childId, 1);
            SkillRoleMgr.savePathFile("[" .. fight.frame .. "][EFFECT]Fighter " .. buff.target.fighterId .. " Buff"
                    .. buff.modelId .. " 光环生效 , 添加 光环实际效果buff:" .. childId);
        end
        local skillBean = buff.skill.skill.skillBean;
        if (skillBean.f_TriggerType == SkillActionTypeEnum.ACTIVE or
                skillBean.f_TriggerType == SkillActionTypeEnum.TRIGGER or
                skillBean.f_TriggerType == SkillActionTypeEnum.PASSIVE) then
            --主动类的技能触发的光环, 需要特殊处理, 这样才能在我 的光环已经施法之后, 检测到后入场的人
            table.insert(source.activHaloBuff, buff);
        end
        return ;
    end
    local skillModelId = buff.skill:getSkillModelId();
    local children = source.haloChildren[skillModelId];
    if (children == nil) then
        children = {};
        source.haloChildren[skillModelId] = children;
    end
    children[buff] = nil;
    if (not  t:validHalo(buff)) then
        return ;
    end
    self:haloEffect(buff, fight);
end

--[[
* 光环是否能对我生效
* @param buff
* @return
]]
function t:validHalo(buff)
    if (buff.source:isDie()) then
        return false;
    end
    local f_buffEffectValue = buff.buffBean.f_BuffEffectValue;
    --        int disConfig = f_buffEffectValue[0];--距离
    --        double dis = toRatio(disConfig);
    --        return DistanceUtil.countDistance(buff.source.getPosition(),buff.target.getPosition()) <= dis;
    local childBuffId = f_buffEffectValue[1];
    local buffBean = BuffManager.getBuffBean(childBuffId, buff.source);
    if (buffBean == nil) then
        return false;
    end
    return BuffConditionType.condition47.action(buff, buffBean.f_BuffAddCondition);
end

--[[
* 光环生效
* @param buff
]]
function t:haloEffect(buff, fight)
    local f_buffEffectValue = buff.buffBean.f_BuffEffectValue;
    local childBuffId = f_buffEffectValue[1];--真正生效的buff
    local resultMap = BuffManager.addBuff(fight, buffskill, buff.source, buff.target, false, childBuffId, 1, buff);
    local o = resultMap[2];
    if (o ~= IBuffEffect.SUCCESS_ATTRI_HAS_CHANGE) then
        return ;
    end
    local addBuff = resultMap[3];
    local children = buff.sourcehaloChildren.get(buff.skill.getSkillModelId());
    children.put(buff, addBuff);
    SkillRoleMgr.savePathFile("[" .. fight.frame .. "][EFFECT]Fighter " .. buff.target.fighterId .. " Buff"
            .. buff.modelId .. " 光环生效 , 添加 光环实际效果buff:" .. addBuff.modelId);
end
--[[
* 光环受益者检测
* @param fight
* @param buff
]]
function t:guestValid(fight, buff)
    local b = t:validHalo(buff);
    local children = buff.source.haloChildren[buff.skill:getSkillModelId()];
    local realBuff = children[buff];
    if (b) then
        if (realBuff == nil) then
            --之前没在我光环里面
            self:haloEffect(buff, fight);
        end
    else
        if (realBuff ~= null) then
            --需要失效
            children[buff] = nil;
            t:removeBuff(realBuff, fight);
        end
    end
end

--[[
* 光环主人检测
* @param fight
* @param buff
]]
function t:masterValid(fight, buff)
    local source = buff.source;
    local children = source.haloChildren[buff.skill:getSkillModelId()];
    if (children == nil) then
        return ;
    end
    local entries = children;

    for k, entry in pairs(entries) do
        local realBuff = entry;
        local b = t:validHalo(k);
        if (b) then
            if (realBuff == null) then
                --之前没在我光环里面
                self:haloEffect(k, fight);
            end
        else
            if (realBuff ~= nil) then
                --需要失效
                entries[k] = nil;
                t:removeBuff(realBuff, fight);
            end
        end
    end
end

function t:remove(fight, buff, force, ...)
    --角色死亡时,也会移除buff(走这个方法), 所以死亡的时候,不需要额外处理了,
    local parameters = {...};
    local source = buff.source;
    local target = buff.target;
    if (source ~= target) then
        --光环的受益者,不需要做额外的处理
        return ClientFight.BaseBuffEffect.remove(self,fight, buff, force, parameters);
    end
    source.getActivHaloBuff().remove(buff);
    local skillModelId = buffskill.getSkillModelId();
    --光环的主人, 移除光环类buff时, 需要把他的孩子们全干掉
    local children = source.haloChildren[skillModelId];
    if (children == nil) then
        return ClientFight.BaseBuffEffect.remove(self,fight, buff, force, parameters);
    end
    local entries = children;
    for k, entry in pairs(entries) do
        local haloBuff = k;
        local child = entry;
        t:removeBuff(haloBuff, fight);
        t:removeBuff(child, fight);
    end
    return ClientFight.BaseBuffEffect.remove(self,fight, buff, force, parameters);
end

--[[
* 移除掉buff
* @param buff
* @param fight
]]
function t:removeBuff(buff, fight)
    if (buff == nil) then
        return ;
    end
    local childTarget = buff.target;
    buffManager.removeFighterBuff(fight, childTarget, buff, true);--
    SkillRoleMgr.savePathFile("[" .. fightframe .. "][EFFECT]Fighter " .. buff.targetfighterId .. " Buff"
            .. buffmodelId .. " 光环失效 , 移除buff:" .. buffmodelId);
end
t.New()